<?php
namespace My\Controller;

use Utuo\BackgroundController;

class WechatActivityBoxController extends BackgroundController
{
    // init
    public function _initialize()
    {
        $this->setControllerTitle('拆盒子');
        $this->setActionTitle(array(
            'index' => '活动管理',
            'add' => '添加活动',
            'recycle' => '回收站',
            'modify' => '修改活动',
            'delete' => '删除活动',
            'host' => '发起活动情况',
            'play' => '抽奖明细',
            'register' => '中奖登记名单',
            'prize' => '奖品设置'
        ));
        
        $this->setActionNav('index,add,recycle');
    }
    
    // prize setting
    public function prizeAction()
    {
        $id = I('id', 0, 'intval') or $this->ajaxError('id不能为空');
        $item = M('WechatActivityBox')->where(array(
            'cid' => $this->aid,
            'id' => $id
        ))->find();
        if (empty($item['prize'])) {
            $item['prize'] = array(
                array(),
                array(),
                array()
            );
        } else {
            $item['prize'] = unserialize($item['prize']);
        }
        $this->assign('item', $item);
        $this->display();
    }
    
//     public function fixCodeAction(){
//         $codeList = M('WechatActivityBoxPrize')->where(array(
//             'cid'=>197,
//             'aid'=>3,
//             'prizeLevel'=>1
//         ))->select();
//         foreach ($codeList as $v){
//             $openid = M('WechatActivityUser')->where(array(
//                 'cid'=>197,
//                 'aid'=>3,
//                 'tag'=>'box',
//                 'id'=> $v['uid']
//             ))->getField('openid');
//             $userId = M('WechatUser')->where(array(
//                 'cid'=>197,
//                 'openId'=>$openid
//             ))->getField("id");
//             $result = M('DistributionShopRedPacket')->where(array(
//             'cid'=>197,
//             'code'=>trim($v['prizeCode']),
//             'uid'=>0
//             ))->save(array(
//             'uid'=>$userId,
//             'takeAwayTime'=>strtotime($v['createTime'])
//             ));
//             dump($userId.'/'.$v['prizeCode'].'/'.$result);
//         }
//     }
    
    // save prize
    public function savePrizeAction()
    {
        $prize = I('prize');
        empty($prize) && $this->ajaxError('数据为空');
        $prize = serialize($prize);
        $id = I('id');
        $model = M('WechatActivityBox');
        $result = $model->where('id=%d AND cid=%d', $id, $this->aid)->setField('prize', $prize);
        if ($result === false) {
            $this->ajaxError('设置失败');
        }
        $this->ajaxSuccess('设置成功');
    }
    
    // getList
    public function getLists($is_deleted)
    {
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $d = D('WechatActivityBox');
        $condition = array(
            'cid' => $this->aid,
            'is_deleted' => $is_deleted
        );
        $list = $d->where($condition)
            ->limit($pagesize)
            ->page($page)
            ->order('id DESC')
            ->select();
        $count = $d->where($condition)->count();
        foreach ($list as &$v) {
            $v['activityUrl'] = $this->getUrl($this->aid, $v['id']);
        }
        $this->assign('itemList', $list);
        $this->showPage($count, $pagesize);
        $this->display();
    }
    
    // 查看发起活动情况
    public function hostAction()
    {
        $id = I('id', 0, 'intval');
        $m = M('WechatActivityBoxHost h');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=h.uid');
        $m->where('h.cid=%d AND h.aid=%d', $this->aid, $id);
        $m->field('h.*, u.nickname,u.headimgurl,u.province,u.city');
        $mCount = clone $m;
        $count = $mCount->count();
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $lists = $m->limit($pagesize)
            ->page($page)
            ->select();
        $playCount = M('WechatActivityUser')->where(array(
            'cid' => $this->aid,
            'aid' => $id,
            'tag' => 'box'
        ))->count();
        $this->assign('hostCount', $count);
        $this->assign('playCount', $playCount);
        $this->assign('lists', $lists);
        $this->showPage($count, $pagesize);
        $this->display();
    }
    
    // 查看参与人员情况
    public function playAction()
    {
        $id = I('id', 0, 'intval');
        $aid = I('aid', 0, 'intval');
        $m = M('WechatActivityBoxPlay p');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=p.uid');
        $m->where('p.cid=%d AND p.aid=%d AND p.hid=%d', $this->aid, $aid, $id);
        $m->field('p.*, u.nickname,u.headimgurl,u.province,u.city');
        $mCount = clone $m;
        $count = $mCount->count();
        $page = $this->getPage();
        $pagesize = $this->getPageSize();
        $lists = $m->limit($pagesize)
            ->page($page)
            ->select();
        $this->assign('lists', $lists);
        $this->showPage($count, $pagesize);
        $this->display();
    }

    // 查看中奖名单情况
    public function getPrizeAction()
    {
        $this->setActionTitle('getPrize','查看中奖名单');
        $levelName = array('一等奖','二等奖','三等奖');
        $prizeCount = array('一等奖'=>0,'二等奖'=>0,'三等奖'=>0);
        $id = I('id', 0, 'intval');
        $m = M('WechatActivityBoxPrize p');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=p.uid');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_BOX_HOST__ h ON h.id=p.hid');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_BOX_REGISTER__ r ON r.uid=p.uid');
        $m->where('p.cid=%d AND p.aid=%d', $this->aid, $id);
        $m->field('p.*, u.nickname,u.headimgurl,u.province,u.city,h.openCount,h.create_time,r.name,r.tel,r.address,r.zip');
        $lists = $m->select();
        foreach ($lists as $k => &$v){
            $v['prizeLevelName'] = $levelName[$v['prizeLevel']];
            empty($v['prizeLevelName']) && $v['prizeLevelName'] = '未中奖';
            $prizeCount[$v['prizeLevelName']]++;
        }
        $this->assign('lists', $lists);
        $this->assign('prizeCount',$prizeCount);
        $this->display();
    }    
    // 查看登记中奖名单情况
    public function registerAction()
    {
        $id = I('id', 0, 'intval');
        $m = M('WechatActivityBoxRegister r');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_USER__ u ON u.id=r.uid');
        $m->join('LEFT JOIN __WECHAT_ACTIVITY_BOX_HOST__ h ON h.id=r.hid');
        $m->where('r.cid=%d AND r.aid=%d', $this->aid, $id);
        $m->field('r.*, u.nickname,u.headimgurl,u.province,u.city,h.money,h.create_time');
        $lists = $m->select();
        $this->assign('lists', $lists);
        $this->display();
    }
    
    // 获取前台活动URL
    protected function getUrl($cid, $aid)
    {
        $url = U('Home/Box/index', array(
            'cid' => $cid,
            'aid' => $aid
        ), true, true);
        return $url;
    }
}